let userId_const = localStorage.getItem("userId");
let userName_const = localStorage.getItem("userName");
let username = $(".dropdown-user-name");
username.find('i').remove();
let user_name = '<i>你好，'+userName_const+'老师</i>';
username.append(user_name);                       //生成导航栏的老师名字
username = $("#user_name");
username.find('span').remove();
user_name = '<span>'+userName_const+'老师</span>'      //生成导航栏下拉框的老师名字
username.append(user_name);

let examId_arr = localStorage.getItem("examId");
let examName_arr = localStorage.getItem("examName");

//获取url中的参数方法
function getUrlParam(name) {
    //构造一个含有目标参数的正则表达式对象
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    //匹配目标参数
    let r = window.location.search.substr(1).match(reg);
    //返回参数
    if (r != null) {
        return unescape(r[2]);
    } else {
        return null;
    }
}

var courseId = getUrlParam("courseId");
var data_length;//需要填写考试要点的课程目标个数
var target_num=[];//课程目标序号
function init(){
    let flag=false;
    let examId_arr_handle;
    examId_arr_handle = examId_arr.split(",")//分割数据
    for(let i=0;i<examId_arr_handle.length;i++){
        if(courseId==examId_arr_handle[i]){
            flag=true;
        }
    }
    if(flag==true){
        $.ajax({
            url:'/method/findById',
            type:'get',
            dataType: 'json',
            contentType:'application/json',
            data:{"courseId":courseId},
            success:function (data){
                //获取课程名
                $.ajax({
                    url: '/lv_course/findNameById',
                    type: 'get',
                    dataType: 'json',
                    async:false,
                    data:{"courseId":courseId},
                    success: function (data) {
                        courseName=data.courseName;
                    }
                });
                $("#courseName").empty();
                $("#courseName").append(courseName);
                let index;//期末考试所在位置
                let all_evaluation_way;//所有的评价方式
                all_evaluation_way=data.evaluaKeywords.split(',');
                for(let i in all_evaluation_way){
                    if (all_evaluation_way[i]==="期末考试")
                        index=i;
                }
                $.ajax({
                    url:'/target/findEvaluationWayById',
                    type: 'get',
                    dataType: 'json',
                    data: {"courseId": courseId},
                    success:function (data){

                        data_length=data.length;
                        let exam_value;
                        $("#exam_point").find('tr').remove();
                        let tr;
                        for (let i in data) {
                            exam_value = data[i].evaluationValue.split(';');
                            target_num.push(data[i].courseTargetNum);
                            if(data[i].examPoint){
                                tr = '<td>' + data[i].courseTargetContent + '</th>'
                                    + '<td class="text-center exam-value">' + exam_value[index] + '</td>'
                                    + '<td><textarea class="form-control" rows="4" aria-label="With textarea">' + data[i].examPoint + '</textarea></td>';
                            }
                            else {
                                tr = '<td>' + data[i].courseTargetContent + '</th>'
                                    + '<td class="text-center exam-value">' + exam_value[index] + '</td>'
                                    + '<td><textarea class="form-control" rows="4" aria-label="With textarea" placeholder="请在此输入考试要点"></textarea></td>';
                            }
                            $("#exam_point").append('<tr>' + tr + '</tr>');
                        }
                    }
                });
            }
        })
    }else
        swal({title:"您未拥有该课程或该课程没有期末考试",type:"warning"});

}

function submit(){
    $("#exam_point tr").each(function (i){
        var exam_point_input=$(this).find("td:last").find("textarea").val();
            console.log(exam_point_input);
            $.ajax({
                url:'/target/updateExamPointById',
                type:'post',
                dataType:'json',
                data:{"courseId":courseId,"courseTargetNum":target_num[i],"examPoint":exam_point_input},
                success:function (){
                    swal({title:"提交成功！",type:"success"});
                }
            })
    });
    init();
}




$(document).ready(function (){

        init();

})